Public Class HotlineChangerSMEReport
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents DataGrid2 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents ContentPaneFooter1 As ccapps.BillingNetControlLibrary.ContentPaneFooter

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private _PointingLinks() As String = New String() {"HotlineChangerSMEReport.aspx?CachingPreventionParameter=" & DateTime.Now.ToString("yyyyMMddHHmmss"), "HotlineChangerSMEReport.aspx?CachingPreventionParameter=" & DateTime.Now.ToString("yyyyMMddHHmmss")}
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PageTitle = "Hotline Changer (SME) Report"
        'Session("user") = "Mehmet"
        If (Not Me.IsPostBack) Then
            If Request.QueryString("ProcessID") Is Nothing Then
                listHistory(0)
            Else
                Me.showProcessDetail(Request.QueryString("ProcessID"), 0)
            End If
        End If

        Me.DataGrid1.Visible = Request.QueryString("ProcessID") Is Nothing
        Me.DataGrid2.Visible = Not Me.DataGrid1.Visible
        Me.ContentPaneFooter1.ConfirmButton.Visible = Me.DataGrid2.Visible
    End Sub

    Private Sub DataGrid1_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        Me.listHistory(e.NewPageIndex)
    End Sub

    Private Sub listHistory(ByVal newPageIndex As Integer)
        Me.DataGrid1.CurrentPageIndex = newPageIndex
        Me.DataGrid1.VirtualItemCount = persistent.Hotline.GetItemCountForProcessType(persistent.Hotline.HotlineChangeProcessType.SME)
        Me.DataGrid1.DataSource = persistent.Hotline.GetHotlineProcessHistory(persistent.Hotline.HotlineChangeProcessType.SME, Session("user"), Me.DataGrid1.CurrentPageIndex, Me.DataGrid1.PageSize)
        Me.DataGrid1.DataBind()
    End Sub

    Private Sub showProcessDetail(ByVal processID As String, ByVal pageIndex As Integer)
        Me.DataGrid2.CurrentPageIndex = pageIndex
        Me.DataGrid2.DataSource = persistent.Hotline.GetHotlineProcessDetail(processID)
        Me.DataGrid2.DataBind()
    End Sub

    Public ReadOnly Property PointingLinks() As String()
        Get
            Return Me._PointingLinks
        End Get
    End Property

    Private Sub DataGrid2_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid2.PageIndexChanged
        Me.showProcessDetail(Request.QueryString("ProcessID"), e.NewPageIndex)
    End Sub

    Private Sub ContentPaneFooter1_Confirmed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1.Confirmed
        Dim currentPageIndex = Me.DataGrid2.CurrentPageIndex
        Me.DataGrid2.AllowPaging = False
        showProcessDetail(Request.QueryString("ProcessID"), 0)

        'Response.Clear()
        'Response.Buffer = True
        'Response.AddHeader("content-disposition", "attachment;filename=Request_" & Request.QueryString("ProcessID") & "_Report.xls")


        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Response.ContentType = "application/vnd.ms-excel"

        Response.Charset = ""

        Me.EnableViewState = False

        Dim stringWrite As New System.IO.StringWriter

        Dim htmlWrite As New HtmlTextWriter(stringWrite)

        Me.DataGrid2.RenderControl(htmlWrite)

        Response.Write(stringWrite.ToString())

        Response.End()
        Me.DataGrid2.AllowPaging = True
        showProcessDetail(Request.QueryString("ProcessID"), currentPageIndex)
    End Sub
End Class
